'''
This is just a little warm up session to test out some ideas for how we are going to work on games for the competition.
The goal of this is to test out: Basic graphics, physics, event handling and game structure
'''
from lepton.particle_struct import Vec3
from pyglet.gl import *
import os
import math
import data
import pyglet
import game
from random import choice, randint


class Enemy:
	def __init__(self, centerx, centery, health, destx, desty, images):
		self.name		= game.enemy_counter
		game.enemy_counter 	+= 1
		self.centerx		= centerx
		self.centery		= centery
		self.health		= health
		self.images		= images
		self.velocity		= Vec3(1, 0, 0) # Currently not being used
		self.speed		= randint(1, 6)
		self.time		= 0
		self.begining		= Vec3(centerx, centery, 0)
		self.destination	= Vec3(destx, desty, 0)
		game.state.add_enemy(self)
		self.alive		= True
		#Temp, clean me up later
		self.start_animation	= pyglet.image.Animation.from_image_sequence(data.return_list_images(), 0.3, True)
		self.sprite		= pyglet.sprite.Sprite(self.start_animation, x = self.centerx, y = self.centery)
		self.height		= self.sprite.height
		self.width		= self.sprite.width
		self.sprite.x		= self.left
		self.sprite.y		= self.bottom

	@property
	def left(self):
		return self.centerx - (self.width // 2)

	@property
	def right(self):
		return self.centerx +  (self.width // 2)

	@property
	def top(self):
		return self.centery + (self.height // 2)

	@property
	def bottom(self):
		return self.centery -  (self.height // 2)

	def linear_tween(self, t, d, c, b):
		return c*t//d + b

	def set_dest(self, destx, desty):
		self.begining.x = self.centerx
		self.begining.y = self.centery
		self.destination.x = destx
		self.destination.y = desty
		self.time = 0
	
	def on_update(self, dt):
		if not self.alive:
			pass

		overlap, corners = game.board.collide( (self.left, self.right, self.top, self.bottom) )
		self.centerx += overlap[0]
		self.centery += overlap[1]
		if (corners[0] & corners[3]):
			self.set_dest(-self.destination.x, self.destination.y)
		
 		if (corners[1] & corners[2]):
			self.set_dest(-self.destination.x, self.destination.y)

		if (corners[0] & corners[1]) and self.time > 0:
			self.set_dest(self.destination.x, -self.destination.y)
		
		if (corners[2] & corners[3]) and self.time > 0:#self.begining.x != self.centerx and self.begining.y != self.centery:
			self.set_dest(self.destination.x, -self.destination.y)

		if self.begining.x != self.centerx and self.begining.y != self.centery and len([i for i in corners if i])==1:

			self.set_dest(-self.destination.x, -self.destination.y)
		
		if self.alive and self.destination.x > 0 and self.left >= self.destination.x:
			print "Enemy got through!!"
			game.score = game.score -1
			self.alive = False
			game.mouse.remove_object(self)
			print "score: ", game.score

		self.time += 1
		changex		= self.destination.x - self.begining.x
		changey		= self.destination.y - self.begining.y
		duration	= math.sqrt( (changex * changex) + (changey * changey))//self.speed

		if not (self.time > duration):
			self.centerx 		= self.linear_tween(self.time, duration, changex, self.begining.x)
			self.centery 		= self.linear_tween(self.time, duration, changey, self.begining.y)
			self.sprite.x		= self.left
			self.sprite.y		= self.bottom

	def selected(self, x, y):
		print "x: ", x, " y: ", y ," left: ", self.left, " right: ", self.right, " top ", self.top, " bottom", self.bottom, " width: ", self.width, " height: ", self.height 
		print " centerx: ", self.centerx, " centery: ", self.centery
		if x >= self.left and x <= self.right and y >= self.bottom and y <= self.top:
			return True
		else:
			return False

	def on_draw(self):
		if self.alive:
			self.sprite.draw()

	def die(self):
		game.mouse.remove_object(self)
		game.score += 1
		self.alive = False
		data.sound('whistle_ding.mp3').play()
		print "score: ", game.score
		del self

	def damage(self, amount):
		self.health -= amount

		if(self.health < 0):
			self.die()

class GameBoard():
	def __init__(self, centerx, centery, width, height): 
		self.width		= width
		self.height		= height
		self.centerx		= centerx
		self.centery		= centery

		self.border = 10
		self.wall_width=10
		self.entrance_size = 100
		self.wall_geometry = []

		ent_wall_height = (self.height - self.entrance_size)/2

		self.wall_geometry.append( [self.border, self.border, self.border, self.border + self.wall_width, self.width - self.border, self.border + self.wall_width, self.width - self.border, self.border] )
		self.wall_geometry.append( [self.border, self.height - self.border, self.border, self.height - self.border - self.wall_width, self.width - self.border, self.height - self.border - self.wall_width, self.width - self.border, self.height - self.border] )

		self.wall_geometry.append( [self.border, self.border, self.border, self.border + ent_wall_height, self.border + self.wall_width, self.border + ent_wall_height, self.border + self.wall_width, self.border] )
		self.wall_geometry.append( [self.width - self.border, self.border, self.width - self.border, self.border + ent_wall_height, self.width - self.border - self.wall_width, self.border + ent_wall_height, self.width - self.border - self.wall_width, self.border] )
		self.wall_geometry.append( [self.border, self.height - self.border, self.border, self.height - self.border - ent_wall_height, self.border + self.wall_width, self.height - self.border - ent_wall_height, self.border + self.wall_width, self.height - self.border] )
		self.wall_geometry.append( [self.width - self.border, self.height - self.border, self.width - self.border, self.height - self.border - ent_wall_height, self.width - self.border - self.wall_width, self.height - self.border - ent_wall_height, self.width - self.border - self.wall_width, self.height - self.border] )


		game.state.add_handler(self)

	@property
	def left(self):
		return self.centerx - (self.width // 2)

	@property
	def right(self):
		return self.centerx +  (self.width // 2)

	@property
	def top(self):
		return self.centery + (self.height // 2)

	@property
	def bottom(self):
		return self.centery -  (self.height // 2)

	def on_draw(self):
		glLoadIdentity()
		glColor4f(0.0, 1.0, 0.0, 1.0)
		for wall in self.wall_geometry:
			pyglet.graphics.draw(len(wall) / 2, GL_POLYGON, ('v2f', wall))


	def collide(self, bounds):
		l, r, t, b = bounds
		
		#which corners collide: tl, tr, br, bl
		corners = [0, 0, 0, 0]
		#amount of overlap: x, y
		overlap = [0, 0]

		for wall in self.wall_geometry:
			#TODO: more general code for this
			x_coords = wall[0:8:2]
			y_coords = wall[1:8:2]
			x_min = min(x_coords)
			x_max = max(x_coords)
			y_min = min(y_coords)
			y_max = max(y_coords)

			if x_min < r and r < x_max:
				if t > y_min and t < y_max:
					corners[1] = x_min
					overlap[0] = -(x_max - r)
					overlap[1] = - (t - y_min)
				if b > y_min and b < y_max:
					corners[2] = x_min
					overlap[0] = -(x_max - r)
					overlap[1] = y_max - b

			if l < x_max and x_min < l:
				if t > y_min and t < y_max:
					corners[0] = x_max
					overlap[0] = (l-x_min)
					overlap[1] =  - (t - y_min)
				if b > y_min and b < y_max:
					corners[3] = x_max
					overlap[0] = (l-x_min)
					overlap[1] = y_max - b

		if sum(corners) > 0:
			#print "Collisions", corners
			if abs(overlap[0]) > 10:
				overlap[0] = 0
			if abs(overlap[1]) > 10:
				overlap[1] = 0
			#print overlap
			
		return overlap, corners


if __name__ == '__main__':
	#Get images for sprites
	game.init()
	#game.state.spawn_enemy(0)
	board = GameBoard(320, 240, 640, 480)
	game.board = board
	pyglet.app.run()
